package cn.atc.controller;

import cn.atc.pojo.*;
import cn.atc.service.AdminService;
import cn.atc.service.ParttypeService;
import cn.atc.service.ProductionplanService;
import cn.atc.service.ProductiveTaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/page")
public class ProductivetaskController {
    @Autowired
    private ProductiveTaskService productiveTaskService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private ProductionplanService productionplanService;

    @Autowired
    private ParttypeService parttypeService;

    @RequestMapping("/productivetask")
    public String toList(){
        return "productivetask";
    }

    @RequestMapping("/productivetask_list")
    @ResponseBody
    public Msg productivetask(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        try {
            //在查询前调用，传入页码以及分页的大小
            PageHelper.startPage(pn, 2);
            //直接进行查询
            List<Productivetask> productivetasks = productiveTaskService.getProductivetaskList();
            //查询后进行包装，并传入需要连续显示的页数
            PageInfo pageInfo = new PageInfo(productivetasks, 1);
            return Msg.success().add("pageInfo", pageInfo);
        } catch (Exception e) {
            return Msg.fail();
        }
    }

    /**
     * 获取单个生产任务书信息，带其他实体信息
     */
    @RequestMapping(value = "/productivetask1", method = RequestMethod.GET)
    @ResponseBody
    public Msg getOne(@RequestParam(value = "id", defaultValue = "1") String id) {
        Productivetask productivetask = productiveTaskService.selectByIdWithEntitys(id);
        System.out.println(productivetask);

        productivetask.setDateStr(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .format(productivetask.getEstimatedTimeOfCompletion()
                        )
        );
        System.out.println(productivetask);
        return Msg.success().add("productivetask", productivetask);

//        try {
//            Productivetask productivetask = productiveTaskService.selectByIdWithEntitys(id);
//            productivetask.setDateStr(
//                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//                            .format(productivetask.getEstimatedTimeOfCompletion()
//                            )
//            );
//            return Msg.success().add("productivetask", productivetask);
//        } catch (Exception e) {
//            return Msg.fail();
//        }
    }

    @RequestMapping(value = "/productivetask_add",method = RequestMethod.POST)
    @ResponseBody
    public Msg save(Productivetask productivetask){
        try {
            String dateStr=productivetask.getDateStr();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            productivetask.setEstimatedTimeOfCompletion(sdf.parse(dateStr));
            productiveTaskService.saveProductiveTask(productivetask);
            return Msg.success();
        }catch (Exception e){
            e.printStackTrace();
            return Msg.fail();
        }

    }

    @RequestMapping(value = "/productivetask_edit/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg update(Productivetask productivetask) {
        try {
            String dateStr=productivetask.getDateStr();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            productivetask.setEstimatedTimeOfCompletion(sdf.parse(dateStr));
            productiveTaskService.updateBySelective(productivetask);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
        return Msg.success();
    }

    @RequestMapping(value = "/productivetask_del/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg delete(@PathVariable("ids") String ids){
        try {
            if(ids.contains("-")){
                //批量删除
                String[] strings = ids.split("-");
                for(String id:strings){
                    productiveTaskService.deleteProductiveTask(id);
                }
            }
            else {
                //单个删除
                productiveTaskService.deleteProductiveTask(ids);
            }
            return Msg.success();
        }catch (Exception e){
            e.printStackTrace();
            return Msg.fail();
        }
    }


    @RequestMapping(value = "/productionplans")
    @ResponseBody
    public Msg getProductionplans(){
        List<Productionplan> productionplans = productionplanService.queryAll();
        return Msg.success().add("productionplans",productionplans);
    }

    @RequestMapping("/partTypes")
    @ResponseBody
    public Msg getPartTypes(){
        List<PartType> partTypes = parttypeService.queryAll();
        return Msg.success().add("partTypes",partTypes);
    }

    @RequestMapping("/productionLeaders")
    @ResponseBody
    public Msg getProductionLeaders(){
        List<Admin> admins = adminService.queryAll();
        return Msg.success().add("admins",admins);
    }

    /**
     * 供其他调用
     * @return
     */
    @RequestMapping("/productiveTaskIds")
    @ResponseBody
    public Msg getAllProductiveTaskId(){
        List<String> productiveTaskIds=productiveTaskService.getAllProductiveTaskId();
        return Msg.success().add("productiveTaskIds",productiveTaskIds);
    }
}
